<?php

namespace App\Http\Controllers;

use App\Mail\RegMail;
use App\Models\Blogs;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{

    public function __construct()
    {
        //定义中间件，排除非登录状态下可以看的
        $this->middleware('auth', [
            'except' => ['index', 'show', 'create', 'confirmmailtoken', 'home', 'store']
        ]);

        $this->middleware('guest', [
            "only" => ['create']
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function index()
    {
        //用户列表
        $users = User::simplePaginate(6);
        return view('user.index', compact('users'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function create()
    {
        return view('user.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param        \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //验证表单
        $data = $this->validate($request, [
            'name' => 'required|min:2',
            'email' => 'required|email|unique:users',
            'password' => 'required|min:5|confirmed'
        ]);
        $data['password'] = bcrypt($data['password']);
        $user = User::create($data);
        //发送邮件
        \Mail::to($user)->send(new RegMail($user));
        session()->flash('success', '请前往邮箱进行验证之后即可登录');
        return redirect()->route('home');
    }

    /**
     * Display the specified resource.
     *
     * @param        int  $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function show(User $user)
    {
        $blogs = Blogs::orderBy('id', 'desc')->where('user_id', $user->id)->simplePaginate(4);
        if(Auth::check()){
            $follow = $user->is_follow(Auth::user()->id) ? '取消关注' : '关注';
        }else{
            $follow = '关注';
        }
        
        return view('user.show', compact('user', 'blogs', 'follow'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param        int  $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function edit(User $user)
    {
        //执行策略
        $this->authorize('update', $user);
        return view('user.edit', compact('user'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param        \Illuminate\Http\Request  $request
     * @param        int  $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
     */
    public function update(Request $request, User $user)
    {
        $this->validate($request, [
            'name' => 'required|min:3',
            'password' => 'nullable|min:5|confirmed'
        ]);
        $user->name = $request->name;
        if ($request->password) {
            $user->password = $request->password;
        }

        $user->save();
        session()->flash('success', '修改成功');
        return redirect()->route('user.index', $user);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param        int  $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
     */
    public function destroy(User $user)
    {
        $this->authorize('delete', $user);
        $user->delete();
        session()->flash('success', "删除成功！");
        return redirect()->route('user.index');
    }

    public function confirmmailtoken($token)
    {
        //修改表的状态
        $user = User::where('email_token', $token)->first();
        if (!$user) {
            session()->flash('danger', '邮箱验证失败！');
            return redirect()->route('home');
        }
        $user->email_active = true;
        $user->save();
        Auth::login($user);
        session()->flash('success', '验证成功,并为你登录');
        return redirect()->route('home');
    }

    public function follow(User $user)
    {
        if (Auth::check())
            $user->followToggle(Auth::user()->id);
        return back();
    }
}
